import axios from 'axios'
import { Toast } from 'vant'
const baseURL = process.env.NODE_ENV === 'production' ? '/api/' : ''

const instance = axios.create({
  baseURL: baseURL,
  timeout: 30 * 1000,
  requestType: 'form'
})

instance.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

instance.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code === 0 || res.code === 200) {
      if (res.hasOwnProperty('data')) return Promise.resolve(res.data)
      else return Promise.resolve(res)
    } else if (res.code === 500) {
      Toast(res.msg)
      return Promise.reject(res)
    } else {
      Toast(res.msg)
      return Promise.reject(res.msg)
    }
  },
  error => {
    Toast('网络错误，请检查您的网络')
    return Promise.reject(error)
  }
)

export default instance
